const { ccclass, property } = cc._decorator;
import ProgressBar from '../ProgressBar';
import loadLobby from './loadLobby';

@ccclass
export default class lobbyRoot extends cc.Component {
    @property(cc.Node)
    bgNode: cc.Node = null;
    @property(cc.Node)
    barNode: cc.Node = null;

    _barControl: ProgressBar = null;
    _lobbyBundle: cc.AssetManager.Bundle = null;
    _gameBundle: cc.AssetManager.Bundle = null;
    _lobbyScale: number = 0;
    _LobbyControl: loadLobby = null;
    _spriteBg: cc.SpriteFrame = null;

    pathLobbyBg: string = "Textures/bg";//大厅背景
    pathLobbyLoadBg: string = "Textures/lobbyBg";//加载大厅背景
    pathLobbyHero: string = "Player/heroA";//Hero皮肤
    pathLobbyHeroJson: string = "Player/Hero";//Hero配置
    pathLobbyLevel: string = "Player/Level";//关卡配置
    pathLobbylevelItem: string = "Prefab/levelItem";//关卡选择按钮预制体
    pathGameSkillIcon: string = "Skills/icons";//技能图标
    pathAwardIcon: string = "Task/awardIcon";//技能图标
    pathGameSkillItem: string = "Skills/skillItem";//技能展示
    pathGameGoodsItem: string = "Store/goods";//商品预制体
    pathGameTaskItem: string = "Task/teskList";//Task预制体
    pathGameHSkillJson: string = "Skills/HSkill";//Hero技能配置文件路径
    pathGameTaskJson: string = "Task/task";//task配置文件路径
    pathGameStoreJson: string = "Store/Store";//商城配置文件路径

    onLoad() {
        let self = this;
        cc.assetManager.loadBundle('lobbyLoadin', function (err, bundle) {
            if (err) {
                return console.error(err);
            }
            self._lobbyBundle = bundle;
            self.LoadLobby();
        });
        this._barControl = this.barNode.getComponent(ProgressBar);
        //获取常驻节点所绑定的脚本 
        this._LobbyControl = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);
    }
    onDestroy() {
        if (this._spriteBg) {
            this._spriteBg.decRef();
            this._spriteBg = null;
        }
    }
    LoadLobby() {
        cc.director.preloadScene("Lobby");
        this.loadBg(0.08);
        this.loadLobbyBg(0.08);
        this.loadHeroDetail(0.07);
        this.loadHeroicon(0.07);
        this.loadSkillIcons(0.08);
        this.loadGameJson(0.07);
        this.loadSkillsJson(0.07);
        this.loadTaskJson(0.07);
        this.loadStoreJson(0.07);
        this.loadAwardcons(0.07)
        this.loadSkillItemPfb(0.07);
        this.loadGoodsItemPfb(0.07);
        this.loadTaskItemPfb(0.07);
        this.loadLevelItemPfb(0.07);
    }

    loadBg(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbyBg, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
                return;
            }
            self._spriteBg = spriteFrame as cc.SpriteFrame;
            self.bgNode.getComponent(cc.Sprite).spriteFrame = self._spriteBg
            self._spriteBg.addRef();
            self.setProgressBar(scale);
        });
    }
    loadLobbyBg(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbyLoadBg, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
                return;
            }
            let sprite = spriteFrame as cc.SpriteFrame;
            self._LobbyControl._LobbyBg = sprite;
            sprite.addRef();
            self.setProgressBar(scale);
        });
    }
    loadHeroicon(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbyHero, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            _atlas.addRef();
            self._LobbyControl._heroIcons = _atlas;
            self.setProgressBar(scale);
        });
    }
    loadHeroDetail(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbyHeroJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._LobbyControl._heroDetails = details;
            self.setProgressBar(scale);
        });
    }
    loadGameJson(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbyLevel, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._LobbyControl._gameJson = details;
            self.setProgressBar(scale);
        });
    }
    loadLevelItemPfb(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathLobbylevelItem, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = assets as cc.Prefab;
            self._LobbyControl._levelItemPfb = details;
            self.setProgressBar(scale);
        });
    }
    loadSkillItemPfb(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameSkillItem, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = assets as cc.Prefab;
            self._LobbyControl.skillItemPfb = details;
            self.setProgressBar(scale);
        });
    }
    loadGoodsItemPfb(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameGoodsItem, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = assets as cc.Prefab;
            self._LobbyControl.GoodsPfb = details;
            self.setProgressBar(scale);
        });
    }
    loadTaskItemPfb(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameTaskItem, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = assets as cc.Prefab;
            self._LobbyControl.TaskPfb = details;
            self.setProgressBar(scale);
        });
    }
    loadSkillIcons(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameSkillIcon, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._LobbyControl.SkillIcons = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadAwardcons(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathAwardIcon, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._LobbyControl._awardIcons = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSkillsJson(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameHSkillJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._LobbyControl.SkillsJson = details;
            self.setProgressBar(scale);
        });
    }
    loadTaskJson(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameTaskJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._LobbyControl.TaskJson = details;
            self.setProgressBar(scale);
        });
    }
    loadStoreJson(scale: number) {
        let self = this;
        this._lobbyBundle.load(this.pathGameStoreJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._LobbyControl._StoreJson = details;
            self.setProgressBar(scale);
        });
    }
    setProgressBar(scale: number) {
        this._lobbyScale = parseFloat((this._lobbyScale + scale).toFixed(3));
        this._barControl.setScale(this._lobbyScale);
        if (this._lobbyScale >= 1) {
            cc.director.loadScene("Lobby");
        }
    }
}
